<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<title>代码生成</title>
<head th:replace="system/inc/incHead :: incHead"></head>
<body class="gray-bg">
<div class="wrapper wrapper-content no-padding no-margins">
    <div class="row no-padding no-margins">
        <div class="col-sm-3 no-padding no-margins">
            <div class="ibox-content p-xxs no-margins m-t-xs">
                <div class="row no-padding no-margins">
                    <div class="form-group col-md-12 no-padding no-margins">
                        <div class="input-group">
                            <span class="input-group-addon">数据源:</span>
                            <input id="dId" name="dId" type="hidden" class="form-control input-sm" th:value="${bizDatasourceConfig.id}">
                            <input id="searchDId" type="text" class="form-control input-sm DatasourceSuggest" th:value="${bizDatasourceConfig.dbName}" refId="#dId" callback="reloadTables()">
                            <div class="input-group-btn">
                                <button type="button" class="btn btn-sm btn-white dropdown-toggle" data-toggle="dropdown">
                                    <span class="fa fa-arrow-down"></span>
                                </button>
                                <button type="button" class="btn btn-sm btn-white ClearDatasourceSuggest" refObj="#searchDId">
                                    <span class="fa fa-remove"></span>
                                </button>
                                <button type="button" class="btn btn-sm btn-white DatasourceSearch">
                                    <span class="fa fa-search"></span>
                                </button>
                                <ul class="dropdown-menu dropdown-menu-right" role="menu"></ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="ibox no-padding no-margins" id="dbTablesBox">
                <div th:unless="${#lists.isEmpty(tableColumns)}" class="ibox-content m-t-xs">
                    <div class="file-manager" id="dbDiv">
                        <h5>表数据</h5>
                        <div class="btn btn-xs btn-success pull-right" id="selectAll" style="margin-top:-20px;"><i class="fa fa-paste">全选</i></div>
                        <div class="btn btn-xs btn-default pull-right m-r-sm" id="clearAll" style="margin-top:-20px;"><i class="fa fa-paste">清空</i></div>
                        <div class="hr-line-dashed"></div>
                        <ul class="folder-list" style="padding: 0" layoutH="128">
                            <li th:each="item:${tableColumns}" class="p-xxs">
                                <a><input type="checkbox" class="i-checks" th:value="${item.name}"><label class="m-l-xs">[[${item.name}]]:【[[${item.comment}]]】</label></a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-6 no-padding no-margins">
            <div class="row no-padding no-margins">
                <div class="col-sm-12 no-margins">
                    <div class="ibox no-padding no-margins">
                        <div class="ibox-title">
                            <div class="btn btn-white btn-bitbucket btn-sm pull-right btnPageReload"><i class="fa fa-refresh">页面刷新</i></div>
                        </div>
                        <div class="ibox-content ljScrollbar3" style="padding:10px;" layoutH="70">
                            <div class="ibox-content">
                                <div class="row">
                                    <div class="form-group col-md-12">
                                        <label>输出路径(<b class="text-danger">*</b>):</label>
                                        <input type="text" class="form-control required" id="outPath" name="outPath" th:value="${CodeGenOutPath}">
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label>包路径(<b class="text-danger">*</b>):</label>
                                        <input type="text" class="form-control required" id="basePackage" name="basePackage" th:value="${BasePackage}">
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label>作者信息(<b class="text-danger">*</b>):</label>
                                        <input type="text" class="form-control required" id="sysAuthor" name="sysAuthor" th:value="${CodeGenAuth}">
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="hr-line-dashed"></div>
                                    <div class="form-group col-md-12">
                                        <div class="btn btn-sm btn-danger pull-right" id="btnCodeGen">生成代码</div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-3 no-padding no-margins">
            <div class="ibox-content p-xxs no-margins m-t-xs">
                <div class="row no-padding no-margins">
                    <div class="form-group col-md-12 no-padding no-margins">
                        <div class="input-group">
                            <span class="input-group-addon">模板组:</span>
                            <input id="gId" name="gId" type="hidden" class="form-control input-sm" th:value="${gId}">
                            <input id="searchGId" type="text" class="form-control input-sm TemplateGroupSuggest" th:value="${bizTemplateGroup.groupName}" refId="#gId" callback="reloadConfig()">
                            <div class="input-group-btn">
                                <button type="button" class="btn btn-sm btn-white dropdown-toggle" data-toggle="dropdown">
                                    <span class="fa fa-arrow-down"></span>
                                </button>
                                <button type="button" class="btn btn-sm btn-white ClearTemplateGroupSuggest" refObj="#searchGId">
                                    <span class="fa fa-remove"></span>
                                </button>
                                <button type="button" class="btn btn-sm btn-white TemplateGroupSearch">
                                    <span class="fa fa-search"></span>
                                </button>
                                <ul class="dropdown-menu dropdown-menu-right" role="menu"></ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="ibox no-padding no-margins">
                <div th:unless="${#lists.isEmpty(templateConfigs)}" class="ibox-content m-t-xs">
                    <div class="file-manager" id="groupDiv">
                        <h5>模板信息</h5>
                        <div class="btn btn-xs btn-success pull-right" id="selectGroupAll" style="margin-top:-20px;"><i class="fa fa-paste">全选</i></div>
                        <div class="btn btn-xs btn-default pull-right m-r-sm" id="clearGroupAll" style="margin-top:-20px;"><i class="fa fa-paste">清空</i></div>
                        <div class="hr-line-dashed"></div>
                        <ul class="folder-list" style="padding: 0" layoutH="128">
                            <li th:each="item:${templateConfigs}" class="p-xxs">
                                <a><input type="checkbox" checked class="i-checks" th:value="${item.name}"><label class="m-l-xs">[[${item.name}]]</label></a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:replace="system/inc/incBottom :: incBottom"></div>
<div th:replace="system/suggest/DatasourceSuggest"></div>
<div th:replace="system/suggest/TemplateGroupSuggest"></div>
<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
    function reloadTables() {
        let compDid = [[${bizDatasourceConfig.id}]];
        let dId = $("#dId").val();
        if (dId != compDid) {
            let dId = $("#dId").val();
            let gId = $("#gId").val();
            window.location.href = "/code/gen?dId=" + dId + "&gId=" + gId;
        }
    }

    function reloadConfig() {
        let compGid = [[${gId}]];
        let gId = $("#gId").val();
        if (gId != compGid) {
            let dId = $("#dId").val();
            let gId = $("#gId").val();
            window.location.href = "/code/gen?dId=" + dId + "&gId=" + gId;
        }
    }

    jQuery(function ($) {
        $(".chosen-select").chosen();
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });
        $(".btnPageReload").on("click", function () {
            let dId = $("#dId").val();
            let gId = $("#gId").val();
            window.location.href = "/code/gen?dId=" + dId + "&gId=" + gId;
        });
        $("#selectAll").on("click", function () {
            $('#dbDiv input[type="checkbox"]').iCheck('check');
        });
        $("#clearAll").on("click", function () {
            $('#dbDiv input[type="checkbox"]').iCheck('uncheck');
        });
        $("#selectGroupAll").on("click", function () {
            $('#groupDiv input[type="checkbox"]').iCheck('check');
        });
        $("#clearGroupAll").on("click", function () {
            $('#groupDiv input[type="checkbox"]').iCheck('uncheck');
        });
        $("#sv").focus();
        $("#GroupsSearch").on("click", function () {
            let sv = $("#sv").val();
            window.location.href = "/template?gid=[[${parent?.id}]]&sv=" + sv;
        });
        $('#sv').on('keypress', function (event) {
            if (event.keyCode == 13) {
                let sv = $("#sv").val();
                window.location.href = "/template?gid=[[${parent?.id}]]&sv=" + sv;
            }
        });
        // 生成代码
        $("#btnCodeGen").on("click", function () {
            let dId = $("#dId").val();
            let gId = $("#gId").val();
            let outPath = $("#outPath").val();
            let basePackage = $("#basePackage").val();
            let sysAuthor = $("#sysAuthor").val();
            let tables = "";
            $('#dbDiv input[type="checkbox"]').each(function () {
                if ($(this).is(':checked')) {
                    tables = tables + "," + $(this).val();
                }
            });
            let temps = "";
            $('#groupDiv input[type="checkbox"]').each(function () {
                if ($(this).is(':checked')) {
                    temps = temps + "," + $(this).val();
                }
            });
            $.ajax({
                url: '/code/gen',
                type: 'post',
                dataType: 'json',
                data: {
                    dId: dId,
                    gId: gId,
                    outPath: outPath,
                    basePackage: basePackage,
                    sysAuthor: sysAuthor,
                    tables: tables,
                    temps: temps
                },
                success: function (rtnObj) {
                    if (rtnObj.statusCode == 200) {
                        // 生成成功
                        swal({
                            title: "代码生成成功",
                            text: "代码生成成功,请查看对应文件夹信息",
                            type: "success"
                        });
                    } else {
                        // 生成失败
                        swal({
                            title: "代码生成失败:" + rtnObj.statusCode,
                            text: rtnObj.message,
                            type: "error"
                        });
                    }
                }
            });
        });
    });
    /*]]>*/
</script>
</body>
</html>